package com.imooc.coupon.filter;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;

@Component
@Slf4j
/**
 * 校验请求中的Token
 */
public class TokenFilter extends AbstractPreZuulFilter {

    @Override
    protected Object cRun() {
        HttpServletRequest request = context.getRequest();
        log.info(String.format("%s request to %s"),
                 request.getMethod(), request.getRequestURL().toString());

        String token = request.getParameter("token");
        if(token == null){
            log.error("error: token is empty ");
            return fali(401, "error: token is empty " );
        }
        return success();
    }

    @Override
    public int filterOrder() {
        //数字越小，会优先执行
        return 1;
    }
}
